package com.sgy.spring_transfer_transaction.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

//@Component
//@Aspect
//@EnableAspectJAutoProxy
public class LogUtils {

    @Pointcut("execution(public void com.sgy.spring_transfer_transaction.service.impl.TransferServiceImpl.transfer(java.lang.String, java.lang.String, int))")
    public void pt1() {}

    /**
     * 业务逻辑开始执行前
     */
    @Before("pt1()")
    public void beforeMethod(JoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            System.out.println(args[i]);
        }
        System.out.println("业务逻辑开始执行之前执行............");
    }

    /**
     * 业务逻辑结束时执行
     */
    @After("pt1()")
    public void afterMethod(){
        System.out.println("业务逻辑结束时执行...............");
    }

    /**
     * 异常时执行
     */
    @AfterThrowing("pt1()")
    public void exceptionMethod(){
        System.out.println("异常时执行...............");
    }

    @AfterReturning(value = "pt1()", returning = "retValue")
    public void successMethod(Object retValue){
        System.out.println("业务逻辑正常时执行...............");
    }

    @Around("pt1()")
    public void arroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕通知beforeMethod...............");
        Object proceed = null;
        try {
            // 控制原有业务逻辑是否执行
            proceed = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
        } catch (Exception e) {
            System.out.println("环绕通知exceptMethod...............");
        } finally {
            System.out.println("环绕通知afterMethod...............");
        }
        System.out.println("环绕通知...............");
    }
}
